<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="laborerId">
        <el-select v-model="queryParams.laborerId" placeholder="请选择劳务人员">
            <el-option
              v-for="dict in laborersList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="发放日期" prop="paymentDate">
        <el-date-picker clearable
          v-model="queryParams.paymentDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发放日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发放方式" prop="paymentMethod">
        <el-select v-model="queryParams.paymentMethod" placeholder="请选择发放方式" clearable>
          <el-option
            v-for="dict in dict.type.lb_salarypayments_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ldcm:salarypayments:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ldcm:salarypayments:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ldcm:salarypayments:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ldcm:salarypayments:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="salarypaymentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" prop="paymentId" />
      <el-table-column label="劳务人员姓名" align="center" prop="name" />
      <el-table-column label="发放日期" align="center" prop="paymentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发放金额" align="center" prop="amount" >
        <template slot-scope="scope">
          <span>{{ scope.row.amount }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="发放方式" align="center" prop="paymentMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lb_salarypayments_method" :value="scope.row.paymentMethod"/>
        </template>
      </el-table-column>

      <el-table-column label="工资条" align="center" prop="payslip" >
        <template slot-scope="scope">
       <el-button @click="handlePreview">查看文件</el-button>
         <el-dialog :visible.sync="dialogVisible" width="60%" @close="handleClose">
             <iframe :src="scope.row.payslip" width="100%" height="600px"></iframe>
         </el-dialog>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lb_salarypayments_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownFile(scope.row.payslip)"
            v-hasPermi="['ldcm:salarypayments:down']"
          >下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改薪资发放管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="laborerId">
          <el-select v-model="form.laborerId" placeholder="请选择劳务人员">
            <el-option
              v-for="dict in laborersList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发放日期" prop="paymentDate">
          <el-date-picker clearable
            v-model="form.paymentDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发放日期">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="发放金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入发放金额" />
        </el-form-item> -->
        <el-form-item label="发放方式" prop="paymentMethod">
          <el-select v-model="form.paymentMethod" placeholder="请选择发放方式">
            <el-option
              v-for="dict in dict.type.lb_salarypayments_method"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="工资条" prop="payslip">
          <el-input v-model="form.payslip" placeholder="请输入工资条" />
        </el-form-item> -->

        <el-form-item label="工资条" prop="payslip">
    <el-button @click="handlePreview">查看文件</el-button>
    <el-dialog :visible.sync="dialogVisible" width="60%" @close="handleClose">
      <iframe :src="fileUrl" width="100%" height="400px"></iframe>
    </el-dialog>
  </el-form-item>
       

        <el-upload
        ref="uploadData"
        :limit="1"
        accept=".docx, .doc, .pdf, .txt, .odt"
        :headers="uploadData.headers"
        :action="uploadData.url + '?updateSupport=' + uploadData.updateSupport"
        :disabled="uploadData.isUploading"
        :on-progress="handleFileUploadDataProgress"
        :on-success="handleFileUploadDataSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入docx, doc, pdf, txt,odt格式文件。</span>
        </div>
      </el-upload>

      <div class="dialog-footer">
        <el-button type="primary" @click="submitFileUploadDataForm">上传文件</el-button>
      </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSalarypayments, getSalarypayments, delSalarypayments, addSalarypayments, updateSalarypayments,listSalaryFinished } from "@/api/ldcm/salarypayments";
import { listLaborers } from "@/api/ldcm/laborers";
import { getToken } from "@/utils/auth";
export default {
  name: "Salarypayments",
  dicts: ['lb_salarypayments_method','lb_salarypayments_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 薪资发放管理表格数据
      salarypaymentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        laborerId: null,
        paymentDate: null,
        paymentMethod: null,
      },
       // 导入图片
       uploadData: {
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/ldcm/salarypayments/upload"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        laborerId: [
          { required: true, message: "劳务人员ID不能为空", trigger: "blur" }
        ],
        paymentDate: [
          { required: true, message: "发放日期不能为空", trigger: "blur" }
        ],
        payslip: [
          { required: true, message: "工资条不能为空", trigger: "blur" }
        ],
      },
      url: null,
      dialogVisible: false,
      fileUrl: '', // 文件的 URL
      laborersList: [],
    };
  },
  created() {
    this.getList();
    this.getLaborers();
  },
  methods: {
    /** 查询薪资发放管理列表 */
    getList() {
      this.loading = true;
      listSalaryFinished(this.queryParams).then(response => {
        this.salarypaymentsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        paymentId: null,
        laborerId: null,
        paymentDate: null,
        amount: null,
        paymentMethod: null,
        payslip: null,
        createBy: null,
        createdAt: null,
        updateBy: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.paymentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加薪资发放管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const paymentId = row.paymentId || this.ids
      getSalarypayments(paymentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改薪资发放管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.paymentId != null) {
            updateSalarypayments(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSalarypayments(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const paymentIds = row.paymentId || this.ids;
      this.$modal.confirm('是否确认删除薪资发放管理编号为"' + paymentIds + '"的数据项？').then(function() {
        return delSalarypayments(paymentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ldcm/salarypayments/export', {
        ...this.queryParams
      }, `salarypayments_${new Date().getTime()}.xlsx`)
    },
      //获取劳务人员
      getLaborers(){
      listLaborers({ pageNum: 1, pageSize: 10000 }).then(response => {
        console.log(response.rows);
        this.laborersList = response.rows;
      });
    },

    //查看文件
    handlePreview() {
      console.log(this.form.payslip);
      // 设置文件 URL
      this.fileUrl = this.form.payslip;
      // 显示对话框
      this.dialogVisible = true;
    },
    handleClose() {
      // 重置状态
      this.fileUrl = '';
      this.dialogVisible = false;
    },
        //图片下载
        handleDownFile(filename){    
     let pos=filename.lastIndexOf("/");
     filename=filename.substring(pos);
     let extendName=filename.substring(filename.indexOf("."))  
     this.download('/ldcm/salarypayments/fileDown', {fileName:filename
      }, `laborer__payslip-${new Date().getTime()}`+extendName)

    },
  }
};
</script>
